package com.zl.pojo;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

public class User implements HttpSessionBindingListener {
    private Integer id;
    private String name;
    private String pwd;
    private Integer age;
    private String loc;
    private Role role;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getLoc() {
        return loc;
    }

    public void setLoc(String loc) {
        this.loc = loc;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", pwd='" + pwd + '\'' +
                ", age=" + age +
                ", loc='" + loc + '\'' +
                ", role=" + role +
                '}';
    }
    @Override
    public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) {
        //当该对象放进去以后触发该方法
        ServletContext application=httpSessionBindingEvent.getSession().getServletContext();
        Integer num=(Integer)application.getAttribute("num");//null
        if(num==null){
            num=1;
        }else{
            num++;
        }
        application.setAttribute("num",num);
    }

    @Override
    public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) {
        //当该对象移除或者session销毁以后触发该方法
        ServletContext application=httpSessionBindingEvent.getSession().getServletContext();
        Integer num=(Integer)application.getAttribute("num");
        num--;
        application.setAttribute("num",num);
    }
}
